﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessInterfaces;
using BusinessCommon;
using System.ComponentModel.Composition;

namespace BusinessCommon
{
    public class CustomerRuleResult:IRuleResult<ICustomer>
    {
        public ICustomer TestedEntity { get; set; }
        public string RuleName { get; set; }
        public string RuleMessage { get; set; }
    }


    [Export(typeof(IBusinessRule<ICustomer>))]
    public class CustomerPhoneNumberRule:IBusinessRule<ICustomer>
    {
        [Import(typeof(IDiagnostics))]
        IDiagnostics _diagnostics;

        public void ProsecuteRules(ref ICustomer Entity, ref List<IRuleResult<ICustomer>> RulesResults)
        {
            if (string.IsNullOrEmpty(Entity.PhoneNumber))
            {
                //rule violation, add to collection
                CustomerRuleResult result = new CustomerRuleResult()
                {
                    TestedEntity = Entity,
                    RuleMessage = "The phone number cannot be null or empty.",
                    RuleName = "Phone Number Required"
                };
                
                //log execution result
                _diagnostics.Log("Phone Number rule violation");
                
                RulesResults.Add(result);

            }
        }
    }
}
